package de.dopichaj.labrador.index;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;

import de.dopichaj.labrador.util.Configuration;
import de.dopichaj.labrador.util.io.ArrayByteReader;
import de.dopichaj.labrador.util.io.ByteReader;


/**
 * An index that is stored in main memory.
 * Obviously you should first create the index and then use the same instance
 * for searching.
 */
public abstract class RAMIndex extends Index {

    private boolean dirty = false;
    private ByteArrayOutputStream tagIdOutput;
    private ByteArrayOutputStream digestOutput;
    private int documentCount;
    private int elementCount;
    private long elementLengthSum;
    
    public RAMIndex(String[] stopWords, Configuration configuration) {
        super(stopWords, configuration);
    }

    @Override
    public boolean isDirty() {
        return dirty;
    }

    @Override
    public void markAsDirty() {
        dirty = true;
    }

    @Override
    public void markAsClean() {
        dirty = false;
    }

    @Override
    public BufferedReader getTagIdReader() {
        return new BufferedReader(new InputStreamReader(
            new ByteArrayInputStream(tagIdOutput.toByteArray())));
    }

    @Override
    public PrintWriter getTagIdWriter() {
        tagIdOutput = new ByteArrayOutputStream();
        return new PrintWriter(tagIdOutput);
    }

    @Override
    public ByteReader getDigestByteReader() {
        return new ArrayByteReader(digestOutput.toByteArray());
    }

    @Override
    public OutputStream getDigestOutputStream() {
        digestOutput = new ByteArrayOutputStream();
        return digestOutput;
    }

    @Override
    public int getDocumentCount() {
        return documentCount;
    }

    @Override
    public int getElementCount() {
        return elementCount;
    }

    @Override
    public long getElementLengthSum() {
        return elementLengthSum;
    }

    @Override
    public void storeMetaData(int documentCount, int elementCount, long elementLengthSum) throws IOException {
        this.documentCount = documentCount;
        this.elementCount = elementCount;
        this.elementLengthSum = elementLengthSum;
    }
}
/*
Copyright (c) 2007 Philipp Dopichaj

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/